package com.mccken.designpattern.strategy.strategy_03;

import com.mccken.designpattern.strategy.strategy_03.sub_srategy.CashbackStrategy;
import com.mccken.designpattern.strategy.strategy_03.sub_srategy.CouponStrategy;
import com.mccken.designpattern.strategy.strategy_03.sub_srategy.EmptyStrategy;
import com.mccken.designpattern.strategy.strategy_03.sub_srategy.GroupbuyStrategy;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 策略枚举
 * 
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-14 21:06
 **/
@AllArgsConstructor
@Getter
public enum  PromotionStrategyEnum {
	
	COUPON("COUPON", CouponStrategy.class),
	CASHBACK("CASHBACK", CashbackStrategy.class),
	EMPTY("EMPTY", EmptyStrategy.class),
	CHILDREN("CHILDREN", CashbackStrategy.class),
	GROUPBUY("GROUPBUY", GroupbuyStrategy.class),
	
	;
	
	private final String promotionKey;
	private final Class strategyClazz;
	
	public static IPromotionStrategy getPromotionStrategy(String promotionKey) {
		PromotionStrategyEnum strategyEnum = prase(promotionKey);
		Class strategy = strategyEnum.getStrategyClazz();
		try {
			return (IPromotionStrategy) strategy.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private static PromotionStrategyEnum prase(String promotionKey) {
		PromotionStrategyEnum defaultStrategy = EMPTY;
		if (StrUtil.isEmpty(promotionKey)) {
			return defaultStrategy;
		}
		for (PromotionStrategyEnum promotionStrategyEnum : PromotionStrategyEnum.values()) {
			if (promotionStrategyEnum.name().equalsIgnoreCase(promotionKey)) {
				return promotionStrategyEnum;	
			}
		}
		
		return defaultStrategy;
	}

	public static void main(String[] args) {
		
		
	}
	
}
